package com.lagou.controller;

import com.lagou.pojo.User;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zy
 * @version 1.0
 * @date 2021/3/16 14:08
 */
@Controller
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index(){
        return "login";
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/welcome")
    public String  welcome() throws IOException {
        return "welcome";
    }

    @RequestMapping("/welcome1")
    public void welcome1(HttpServletResponse response,String email,String password) throws IOException {
        User user = userService.checkEmail(email);
        boolean b =  user.isStatus();
        Cookie cookie = new Cookie("token",user.getToken());
        response.addCookie(cookie);
        response.getWriter().write(String.valueOf(b));
    }

    @RequestMapping("/register1")
    public void register1(HttpServletResponse response,String email,String password,String code) throws IOException {
        Integer registered = userService.registered(email, password, code);
        response.getWriter().write(String.valueOf(registered));
    }

    @RequestMapping("/token")
    public void token(HttpServletResponse response,String token) throws IOException {
        String emailByToken = userService.getEmailByToken(token);
        response.getWriter().write(emailByToken);
    }

}
